package com.dgut.edu.cn.design.mode.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

/**
 * @description:
 * @author: hongbin.zheng
 * @create: 2023-08-11 11:26
 **/
public class CfgCenterUtils {

	public static String getCfgString(String configKey, String defaultValue) {
		// 获取资源文件的 InputStream
		String resource = String.format("%s.json", configKey);
		InputStream inputStream = CfgCenterUtils.class.getResourceAsStream("/cfg/" + resource);

		if (inputStream != null) {
			try {
				// 使用 InputStreamReader 读取 InputStream
				InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

				// 使用 BufferedReader 读取内容
				BufferedReader bufferedReader = new BufferedReader(reader);
				StringBuilder jsonString = new StringBuilder();
				String line;
				while ((line = bufferedReader.readLine()) != null) {
					jsonString.append(line);
				}

				// 输出解析结果
				/*System.out.println("Parsed JSON string: " + jsonString.toString());*/
				return jsonString.toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			//System.out.println("Resource not found.");
			return defaultValue;
		}
		return defaultValue;
	}
}
